//
//  AlertView.m
//  HappyBug
//
//  Created by liulei on 2018/11/6.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "AlertView.h"
#import "AlertViewTableViewCell.h"
#define  WHITH   [UIScreen mainScreen].bounds.size.width
#define  HEIGHT  [UIScreen mainScreen].bounds.size.height
#import "Person.h"
@implementation AlertView{
    
    NSArray *dataArray;
    
    NSArray *imageArray;
    
    NSMutableArray *personArray;
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        [self initData];

        [self initView];
        
    }
    return self;
}
-(void)initData{
    personArray =[[NSMutableArray alloc]init];
    for(int i=0;i<2;i++){
        Person *p=[[Person alloc]init];
        p = [p CreatePersonModelWithName:[NSString stringWithFormat:@"北京人%d",i] Address:@"北京" Sex:@"男"];
        [personArray addObject:p];
        
    }
}
-(void)initView{
    _mainView =[[UIView alloc]init];
    [_mainView setBackgroundColor:[UIColor whiteColor]];
    _mainView.frame=CGRectMake(40,HEIGHT/5, WHITH-40*2,210);
    _mainView.layer.cornerRadius=6;
    _mainView.layer.borderWidth=0.2;
    [self addSubview:_mainView];
    
    UILabel *label = [[UILabel alloc]init];
    [label setBackgroundColor:[UIColor grayColor]];
    label.frame=CGRectMake(0, 0,_mainView.frame.size.width, 50);
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor =[UIColor blackColor];
    label.text=@"请选择";
    [_mainView addSubview:label];
    
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(0, label.frame.origin.x+label.frame.size.height+1,  _mainView.frame.size.width, 1)];
    lineView.alpha=0.2;
    lineView.layer.borderWidth=0.2;
    [lineView setBackgroundColor:[UIColor grayColor]];
    [_mainView addSubview:lineView];
    
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,label.frame.origin.x+label.frame.size.height+2,  _mainView.frame.size.width, 100) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    [self.tableView setBackgroundColor:[UIColor redColor]];
    self.tableView.dataSource=self;
    self.tableView.scrollEnabled=NO;
    [_mainView addSubview:self.tableView];
    
    
    CGFloat hei =(_mainView.frame.size.height+_mainView.frame.origin.y)-(self.tableView.frame.origin.y+self.tableView.frame.size.height)-40+10;
    UIButton *leftbt=[[UIButton alloc]initWithFrame:CGRectMake(10,hei, _mainView.frame.size.width/2-20, 40)];
    [leftbt setTitle:@"确认" forState:UIControlStateNormal];
    [leftbt setBackgroundColor:[UIColor grayColor]];
    [leftbt addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_mainView addSubview:leftbt];
    
    
    
    UIButton *rightbt=[[UIButton alloc]initWithFrame:CGRectMake(_mainView.frame.size.width-(_mainView.frame.size.width/2),hei, _mainView.frame.size.width/2-10, 40)];
    [rightbt setTitle:@"取消" forState:UIControlStateNormal];
    [rightbt setBackgroundColor:[UIColor grayColor]];
    [rightbt addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    [_mainView addSubview:rightbt];
    
}
-(void)rightClick{
    
    //移除keywindows 上面的自己view
    [self removeFromSuperview];
}

-(void)leftClick{
    //移除keywindows 上面的自己view
    [self removeFromSuperview];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  [personArray count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    
    //重用
    AlertViewTableViewCell *selfcell=[tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果cell为nil（缓存池找不到对应的cell）
    if (selfcell == nil) {
        selfcell = [[AlertViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    Person  *pp =[personArray objectAtIndex:indexPath.row];
    
    [selfcell setValueWithArray:pp];
    return  selfcell;
    
}


-(void)showAlert{
    
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    _mainView.alpha = 0.0;
    [UIView animateWithDuration:0.05 animations:^{
        _mainView.alpha = 1;
    }];}
@end
